/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/pgl.h>
#include <pgl/core/PGLObjectRegistry.h>
#include <pgl/core/PGLRegion.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

namespace pgl
{

CObjectRegistry::CObjectRegistry() 
{
	PGL_TRACE(_T("pgl::ObjectRegistry, starting\n"));
};

CObjectRegistry::~CObjectRegistry()
{
	m_registry.clear();
	PGL_TRACE(_T("pgl::ObjectRegistry, closing\n"));
}

void CObjectRegistry::RegisterObjectP(LPCTSTR szName, CPGLNodePtr (*fcn)(void))
{
	ASSERT(fcn);

	CRegistry::iterator it= m_registry.find(szName);
	if ( it == m_registry.end())
		m_registry[szName]=fcn;
};


CPGLNodePtr CObjectRegistry::GetObjectP(LPCTSTR szName)
{
	CRegistry::iterator it=m_registry.find(szName);
	if ( it == m_registry.end())
	{
		PGL_TRACE(_T("GetObject Failed\n"));
		return CPGLNodePtr();
	}
	else
	{
		ASSERT(it->second);
		return (it->second)();
	}
}

size_t CObjectRegistrySingleton::GetCount()
{	
	return Instance()->GetCountP();
};
void CObjectRegistrySingleton::RegisterObject(LPCTSTR szName, CPGLNodePtr (*fcn)(void))
{
	Instance()->RegisterObjectP(szName, fcn);
}

CPGLNodePtr CObjectRegistrySingleton::GetObject(LPCTSTR szName)
{
	return Instance()->GetObjectP(szName);
}

};
